import datetime

from rest_framework.authentication import BaseAuthentication
from  utils.toojwt import tooljwt
from rest_framework.exceptions import AuthenticationFailed
from rest_framework.response import Response
from user import models

import jwt

class JWTAuthenticationss(BaseAuthentication):
    # 重写验证
  def authenticate(self, request):
    tokendata = request.META.get('HTTP_AUTHORIZATION','你好')
    if tokendata:
        token = tokendata.split(' ')[1]

        try:
            userdata = tooljwt.checktoken(token)
            user = userdata.get('userid')
            expiration = userdata.get('exp')
            headimg: userdata.get('headimg')
            print(datetime.datetime.fromtimestamp(expiration))
            return user, token
        except jwt.ExpiredSignatureError:
            raise AuthenticationFailed('token过期')

        except jwt.InvalidTokenError:
            # if userdata:
            #     userid = userdata.get('userid')
            #     user = models.UserModel.objects.filter(id=userid)
            #     return user,token
            raise AuthenticationFailed('无效的token')


    else:
        return None ,None
    # 重写头部
  def authenticate_header(self, request):


      return 'Bearer realm= "api"'












